//
//  MSAIManager.m
//  MagicSword
//
//  Created by Netease on 15/11/27.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSAIManager.h"
#import "MSGameManager.h"
#import "MSEnemySprite.h"
#import "MSHeroSprite.h"
#import "MSSelectSprite.h"

@implementation MSAIInfo

@end

@implementation MSAIManager

//+ (id)shareManager {
//    static MSAIManager *aiMgr = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        aiMgr = [[MSAIManager alloc] init];
//    });
//    return aiMgr;
//}

+ (MSAIInfo *)AIWithType:(MSAIManagerType)type enmey:(MSEnemySprite *)sprite {
    MSAIInfo *aiInfo = [[MSAIInfo alloc] init];
    aiInfo.enemy = sprite;
    CGPoint curCoord = [[MSGameManager shareManager] CoordFromPoint:sprite.position];
    NSArray *canMoveArray = [[MSGameManager shareManager] getHeroMoveArray:curCoord canMoveValue:sprite.enemyInfo.moveDistance];
    MSHeroSprite *heroSprite;
    aiInfo.curCoord = curCoord;
    aiInfo.moveCoord = curCoord;
    //距离排序
    canMoveArray = [canMoveArray sortedArrayUsingComparator:^NSComparisonResult(MSSelectSprite *obj1, MSSelectSprite *obj2) {
        CGPoint p1 = [obj1 coord];
        CGPoint p2 = [obj2 coord];
        
        CGFloat d1 = sqrt((p1.x-curCoord.x)*(p1.x-curCoord.x)+(p1.y-curCoord.y)*(p1.y-curCoord.y));
        CGFloat d2 = sqrt((p2.x-curCoord.x)*(p2.x-curCoord.x)+(p2.y-curCoord.y)*(p2.y-curCoord.y));
        
        if (d1<d2) {
            return NSOrderedAscending;
        } else if(d1>d2) {
            return NSOrderedDescending;
        }
        return NSOrderedSame;
    }];
    
    switch (type) {
        case MSAIManagerType_Simple:
            aiInfo.moveCoord = curCoord;
            heroSprite = [[MSGameManager shareManager] getEnemyAttackWithCoord:curCoord canAttackValue:sprite.enemyInfo.attackDistance];
            if (heroSprite) {
                aiInfo.attackHero = heroSprite;
            }
            break;
            
        case MSAIManagerType_Easy:
            heroSprite = [[MSGameManager shareManager] getEnemyAttackWithCoord:curCoord canAttackValue:sprite.enemyInfo.attackDistance];
            if (heroSprite) {
                aiInfo.attackHero = heroSprite;
            } else {
                for (MSSelectSprite *value in canMoveArray) {
                    if (value.status == MSSeleteSpriteStatus_Normal) {
                        heroSprite = [[MSGameManager shareManager] getEnemyAttackWithCoord:[value coord] canAttackValue:sprite.enemyInfo.attackDistance];
                        if (heroSprite) {
                            aiInfo.attackHero = heroSprite;
                            aiInfo.moveCoord = [value coord];
                            break;
                        }
                    }
                }
            }
            break;
            
        case MSAIManagerType_Normal:
            heroSprite = [[MSGameManager shareManager] getEnemyAttackWithCoord:curCoord canAttackValue:sprite.enemyInfo.attackDistance];
            if (heroSprite) {
                aiInfo.attackHero = heroSprite;
            } else {
                for (MSSelectSprite *value in canMoveArray) {
                    if (value.status == MSSeleteSpriteStatus_Normal) {
                        heroSprite = [[MSGameManager shareManager] getEnemyAttackWithCoord:[value coord] canAttackValue:sprite.enemyInfo.attackDistance];
                        if (heroSprite) {
                            aiInfo.attackHero = heroSprite;
                            aiInfo.moveCoord = [value coord];
                            break;
                        }
                    }
                }
            }
            
            if (aiInfo.attackHero == nil) {
                aiInfo.moveCoord = [[MSGameManager shareManager] moveEnemyNearByHero:curCoord canMoveValue:sprite.enemyInfo.moveDistance];
            }
            break;
        case MSAIManagerType_Hard:
            // 这个AI有点复杂，先放在
            break;
            
        default:
            break;
    }
    return aiInfo;
}

@end
